package com.guagua.util;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutorService;

import com.hazelcast.client.ClientConfig;
import com.hazelcast.client.HazelcastClient;

/**
 * 封装hazelcast的初始化常规操作
 * <p>
 * 其他开发人员使用hazelcast操作数据时无需关心hazelcast端api
 * <p>
 * 
 * @author 刘时运(shiyun@17guagua.com)
 * @version 2012-4-28 上午11:55:39
 */
public class HazelcastConfigUtil {

	private static HazelcastClient hazelcastClient;
	private static HazelcastClient hazelcastClientOther;
	static ExecutorService es;

	/**
	 * 获取HazelcastClient
	 * <p>
	 * 从classpath目录下加载hazelcastClient.Properties配置文件初始化HazelcastClient
	 * <p>
	 * 如果已经初始化过则直接跳过
	 * 
	 * @return HazelcastClient
	 */
	public static HazelcastClient getClientInstance() {
		if (null == hazelcastClient) {
			String synStrTemp = "this str for syn hazelcastClient init ";
			synchronized (synStrTemp.intern()) {
				if (null == hazelcastClient) {
					hazelcastClient = HazelcastClient.newHazelcastClient(init());
				}
			}
		}
		if (!hazelcastClient.isActive()) {
			String synStrTemp = "this str for syn hazelcastClient init ";
			synchronized (synStrTemp.intern()) {
				if (!hazelcastClient.isActive()) {
					hazelcastClient = HazelcastClient.newHazelcastClient(init());
				}
			}
		}
		return hazelcastClient;
	}

	static ExecutorService getExecutorService() {
		if (null == es) {
			es = getClientInstance().getExecutorService();
		}
		return es;
	}

	/**
	 * 加载配置文件初始化ClientConfig
	 * 
	 * @return ClientConfig
	 */
	private static ClientConfig init() {
		ClientConfig clientConfig = new ClientConfig();
		// 加载配置文件
		Properties systemProperties = new Properties();
		try {
			systemProperties.load(HazelcastConfigUtil.class.getResourceAsStream("/system.properties"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
//		clientConfig.getGroupConfig().setName(systemProperties.getProperty("hazelcast_group_name"));
//		clientConfig.getGroupConfig().setPassword(systemProperties.getProperty("hazelcast_group_pwd"));
		clientConfig.addAddress(systemProperties.getProperty("hazelcast_server").split(","));
		return clientConfig;
	}

	public static HazelcastClient startClientTest() {
		return getClientInstance();
	}

	public static HazelcastClient getClientInstanceOther() {
		if (null == hazelcastClientOther) {
			String synStrTemp = "this str for syn hazelcastClient init ";
			synchronized (synStrTemp.intern()) {
				if (null == hazelcastClientOther) {
					hazelcastClientOther = HazelcastClient.newHazelcastClient(initOther());
				}
			}
		}
		if (!hazelcastClientOther.isActive()) {
			String synStrTemp = "this str for syn hazelcastClient init ";
			synchronized (synStrTemp.intern()) {
				if (!hazelcastClientOther.isActive()) {
					hazelcastClientOther = HazelcastClient.newHazelcastClient(initOther());
				}
			}
		}
		return hazelcastClientOther;
	}

	private static ClientConfig initOther() {
		ClientConfig clientConfig = new ClientConfig();
		// 加载配置文件
		Properties systemProperties = new Properties();
		try {
			systemProperties.load(HazelcastConfigUtil.class.getResourceAsStream("/system.properties"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		clientConfig.getGroupConfig().setName(systemProperties.getProperty("hazelcast_group_name"));
		clientConfig.getGroupConfig().setPassword(systemProperties.getProperty("hazelcast_group_pwd"));
		clientConfig.addAddress(systemProperties.getProperty("hazelcast_server_other"));
		return clientConfig;
	}

}
